'''
Created on Dec 2, 2012

@author: quirino
'''
import unittest
from cupy.messages import Request, Response
from cupy.enums import Options
from StringIO import StringIO

class RequestTest(unittest.TestCase):
    '''
    Unit test for Request object
    '''
    def setUp(self):
        self.req = Request(url="", qs={'q':'pycurl', 'id':3},
                hdr={'Content-Type':'text/xml','Accept':'*/*'},
                opt={Options.ALLOWREDIRECT:False,Options.TIMEOUT:10})
        
    def test_get_set_qs(self):
        qs = self.req.get_qs()
        self.assertIsNotNone(qs, "Invalid request init qs is None")
        self.assertTrue('q' in qs)
        self.assertEquals("pycurl", qs['q'])
        self.assertTrue('id' in qs)
        self.assertEquals(3, qs['id'])
        
    def test_get_set_headers(self):
        hs = self.req.get_headers()
        self.assertIsNotNone(hs)    
        self.assertTrue('Content-Type: text/xml' in hs)
        self.assertTrue('Accept: */*' in hs)
    
    def test_get_set_options(self):
        os = self.req.get_options()
        self.assertIsNotNone(os)
        self.assertTrue(Options.ALLOWREDIRECT in os)
        self.assertEquals(False, os[Options.ALLOWREDIRECT])
        self.assertTrue(Options.TIMEOUT in os)
        self.assertEquals(10, os[Options.TIMEOUT])
        
class ResponseTest(unittest.TestCase):
    
    def test_build_headers(self):
        headers = StringIO()
        headers.write("HTTP/1.1 200 OK\r\n")
        headers.write("Content-Type: text/plain\r\n")
        headers.write("Content-Encoding: gzip\r\n")
        headers.write("Location: http://localhost:8000\r\n")
        
        resp = Response("", headers, None, None)
        _map = resp._build_headers(headers)
        self.assertIsNotNone(_map, "Got None map expected a valid one");
        
        ct = _map["Content-Type"]
        self.assertIsNotNone(ct, "Got NOne value for content type")
        self.assertEquals("text/plain", ct, 
                          "Expected content type: text/plain, got %s" % ct)
        
        ce = _map["Content-Encoding"]
        self.assertIsNotNone(ce, "Got None value for content Encoding")
        self.assertEquals("gzip", ce, 
                          "Expected content Encoding: gzip, got %s" % ce)
        
        loc = _map["Location"]
        self.assertIsNotNone(loc, "Got None value for location")
        self.assertEquals("http://localhost:8000", loc, 
            "Expected location: http://localhost:8000, got %s" % loc)
        
        code = _map['code']
        self.assertIsNotNone(code, "Code cannot be None")
        self.assertEquals("200", code, "Expected code: 200, got %s" % code)
        
        version = _map['version']
        self.assertIsNotNone(version, "version cannot be None")
        self.assertEquals("HTTP/1.1", version, 
                          "Expected version: HTTP/1.1, got %s" % version)
        
        message = _map['message']
        self.assertIsNotNone(message, "message cannot be None")
        self.assertEquals("OK", message, 
                          "Expected message: OK, got %s" % message)
        
if __name__ == '__main__':
    unittest.main()